package gw.com.android.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;
import www.com.library.view.RecycleViewDivider;
import www.com.library.view.RecyclerClickListener;
import www.com.library.view.TintImageTextView;


/**
 * Created by jay on 2017/4/10.
 */
public class BottomChangeAccountDialog extends Dialog{

    private static BottomChangeAccountDialog mInstance = null;
    private DataItemResult mResult;
    private TextView mLoginView;
    private String curName = "";
    private Activity activity;
    protected RecyclerView popListView;// pop上的listview
    protected RecyclerClickListener callback;// 点击事件的回调函数
    protected RecyclerView.Adapter modeListAdapter;


    public BottomChangeAccountDialog(Activity activity, String curName, RecyclerClickListener callback) {
        super(activity, R.style.BottomAnimDialogStyle);
        this.curName = curName;
        this.callback = callback;
        this.activity = activity;
        initView();
    }

    /**
     * 显示弹层
     *
     * @param act
     */
    public static void showBottomChangeAccountDialog(Activity act, String curName, RecyclerClickListener callback) {
        mInstance = new BottomChangeAccountDialog(act,curName,callback);
        mInstance.show();
    }

    private void initView() {
        LayoutInflater inflater = getLayoutInflater();
        if (inflater != null) {
            View rootView = inflater.inflate(R.layout.dialog_account_change, null, false);
            createChangeAccountDialog(rootView);

            Window window = this.getWindow();
            if (window != null) { //设置dialog的布局样式 让其位于底部
                window.setGravity(Gravity.BOTTOM);
                window.getDecorView().setPadding(0,0,0,0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

                lp.y = DeviceUtil.instance().dip2px(0,activity); //设置居于底部的距离
                window.setAttributes(lp);
            }
            setContentView(rootView);
        }


    }

    private void createChangeAccountDialog(View dialogView) {
        setArrayData();

        popListView = (RecyclerView) dialogView.findViewById(R.id.pop_list);
        modeListAdapter = new AccountAdapter(activity);
        // 线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        // 设置布局管理器
        popListView.setLayoutManager(layoutManager);
        popListView.addItemDecoration(new RecycleViewDivider(activity, LinearLayoutManager.VERTICAL, false));

        dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hidden();
            }
        });
        mLoginView = (TextView) dialogView.findViewById(R.id.login_btn);

        if(mResult.getDataCount() < 1){
            mLoginView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hidden();
                    GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_REAL);
                    ActivityManager.backLogin(activity, false);
                }
            });
            mLoginView.setVisibility(View.VISIBLE);
            popListView.setVisibility(View.GONE);
        }else{
            popListView.setAdapter(modeListAdapter);
            mLoginView.setVisibility(View.GONE);
            popListView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏界面
     *
     * @author jay
     */
    private void hidden(){
        if(null != mInstance){
            mInstance.dismiss();
        }
        mInstance = null;
    }
    /**
     * 设置界面数据
     */
    private void setArrayData() {
        mResult = new DataItemResult();
        ArrayList<String> mLoginList = GTConfig.instance().getLoginList();
        DataItemDetail item = null;
        String password = "";

        if (null != mLoginList && mLoginList.size() > 0) {
            for (int i = 0; i < mLoginList.size(); i++) {
                item = new DataItemDetail();
                password = GTConfig.instance().getLoginPass(mLoginList.get(i));
                if (!curName.equals(mLoginList.get(i)) && null != password && password.length() > 0) {
                    item.setStringValue("name", mLoginList.get(i));
                    item.setStringValue("password", password);
//                    item.setIntValue("type",GTConfig.ACC_TYPE_REAL);
                    item.setStringValue("title", mLoginList.get(i));
                    item.setIntValue("iconID", R.mipmap.a_profiles_small);
                    mResult.addItem(item);
                }
            }
        }
    }

        /**
         * 分享类型列表适配器
         * @author reeta
         *
         */
        public class AccountAdapter extends RecyclerView.Adapter {
            private LayoutInflater mInflater;

            public AccountAdapter(Activity act) {
                mInflater = LayoutInflater.from(act);
            }

            public DataItemDetail getItem(int position) {
                if (position < 0 || position >= mResult.getDataCount())
                    return null;
                return mResult.getItem(position);
            }

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View itemView = mInflater.inflate(R.layout.list_item_account_change, parent, false);
                return new ItemView(itemView);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ItemView viewHold = (ItemView) holder;
                if (viewHold.mView != null) {
                    DataItemDetail mItem = getItem(position);
                    String title = mItem.getString("title");
                    if (DeviceUtil.instance().isPhone(title)) {
                        viewHold.mView.setText(title);
                    } else {
                        if (title.startsWith(ConfigUtil.instance().mConfigObject.optString(ConfigType.PREFIX_REAL_ACCOUNT))) {
                            viewHold.mView.setText(AppMain.getAppString(R.string.system_account_change_real, title));
                        } else if (title.startsWith(ConfigUtil.instance().mConfigObject.optString(ConfigType.PREFIX_DEMO_ACCOUNT))) {
                            viewHold.mView.setText(AppMain.getAppString(R.string.system_account_change_demo, title));
                        }
                    }
                    viewHold.mView.setImageResource(mItem.getInt("iconID"));
                    viewHold.mView.setTag(position);
                }
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public int getItemCount() {
                if (null != mResult) {
                    return mResult.getDataCount();
                }
                return 0;
            }

            class ItemView extends RecyclerView.ViewHolder {

                @BindView(R.id.item_title)
                TintImageTextView mView;

                public ItemView(View itemView) {
                    super(itemView);
                    ButterKnife.bind(this, itemView);
                }

                @OnClick(R.id.item_layout)
                void onClick() {
                    if (CommonUtils.isFastDoubleClick()) {
                        return;
                    }
                    if (null != callback) {
                        int position = (int) mView.getTag();
                        hidden();
                        callback.onClick(position, getItem(position));
                    }
                }
            }
        }
}
